import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/*
*
*
* 我的--编辑资料
*
* */
class EditDataPage extends StatefulWidget {
  const EditDataPage({Key key,this.nickName}) : super(key: key);

  //获取编辑昵称的新昵称
  final String nickName;

  @override
  _EditDataPageState createState() => _EditDataPageState();
}

class _EditDataPageState extends State<EditDataPage> {
  ///编辑资料title
  List<String> editTitleList = ["昵称", "性别", "生日", "地区", "简介"];

  ///编辑资料的内容
  List<String> editValeu = ["螃蟹的拉力大小与我的爱还有", "女", "1990 09 09", "四川 成都", ""];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //替换现有数据
    if(widget.nickName!=null){
      editValeu[0]=widget.nickName;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBarWiget(),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          //头像
          nickName(),
          //编辑列表
          editValueList(),
        ],
      ),
    );
  }

  ///APPBar头部状态栏
  AppBar appBarWiget() {
    return AppBar(
      ///去掉APPBar的箭头
      automaticallyImplyLeading: false,
      backgroundColor: Colors.white,
      elevation: 0,
      leading: GestureDetector(
        onTap: () {
          Navigator.pop(context);
        },
        child: Container(
          child: Icon(
            Icons.arrow_back_ios,
            color: Colors.black,
            size: 24,
          ),
        ),
      ),
      centerTitle: true,
      title: Text(
        "编辑资料",
        style: TextStyle(fontSize: 18, color: Color.fromRGBO(17, 17, 17, 1)),
      ),
      actions: [
        Container(
          width: MediaQuery.of(context).size.width - 299,
          padding: EdgeInsets.only(top: 8.5, bottom: 7.5),
          child: GestureDetector(
            onTap: () {
              print("-------------------保存--------------------");
            },
            child: Container(
              margin: EdgeInsets.only(right: 16),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Color.fromRGBO(255, 64, 89, 1),
                borderRadius: BorderRadius.circular(14),
              ),
              child: Text(
                "保存",
                style: TextStyle(fontSize: 14, color: Colors.white),
              ),
            ),
          ),
        ),
      ],
    );
  }

  ///头像
  Widget nickName() {
    return Container(
      width: 71,
      height: 71,
      margin: EdgeInsets.only(top: 16),
      child: Stack(
        children: <Widget>[
          Container(
            width: 71,
            height: 71,
            child: ClipOval(
              child: Image.asset(
                "assets/login_bg.png",
                fit: BoxFit.cover,
              ),
            ),
          ),
          Container(
            alignment: Alignment.bottomRight,
            child: ClipOval(
              child: Image.asset(
                "assets/personal_home_icon/weight_background.png",
                height: 23,
                width: 23,
                fit: BoxFit.cover,
              ),
            ),
          ),
        ],
      ),
    );
  }

  ///editValueItem----编辑内容item
  Widget editValueItem(int i) {
    return Container(
      height: 48,
      width: MediaQuery.of(context).size.width - 32,
      margin: EdgeInsets.only(left: 16, right: 16),
      child: Row(
        children: <Widget>[
          Text(
            editTitleList[i],
            style:
                TextStyle(fontSize: 16, color: Color.fromRGBO(17, 17, 17, 1)),
          ),
          Container(
              width: MediaQuery.of(context).size.width - 76 - 46,
              margin: EdgeInsets.only(left: 28),
              child: editValeu[i].isEmpty
                  ? Text(
                      "去编辑",
                      style: TextStyle(
                          fontSize: 16,
                          color: Color.fromRGBO(153, 153, 153, 1)),
                    )
                  : Text(
                      editValeu[i],
                      style: TextStyle(
                          fontSize: 16, color: Color.fromRGBO(51, 51, 51, 1)),
                    )),
          Spacer(),
          GestureDetector(
            onTap: () {
              switch (i) {
                case 0:
                  Navigator.push(context, MaterialPageRoute(builder: (context) {
                    return InputUnickName();
                  }));
                  break;
                case 1:
                  print("------------------单击了${editTitleList[i]}------------");
                  break;
                case 2:
                  print("------------------单击了${editTitleList[i]}------------");
                  break;
                case 3:
                  print("------------------单击了${editTitleList[i]}------------");
                  break;
                case 4:
                  print("------------------单击了${editTitleList[i]}------------");
                  break;
              }
            },
            child: Container(
              width: 18,
              height: 18,
              child: Icon(
                Icons.arrow_forward_ios_sharp,
                size: 18,
              ),
            ),
          ),
        ],
      ),
    );
  }

  ///editValueList----编辑内容的List
  Widget editValueList() {
    return Expanded(
      child: ListView.builder(
        reverse: false,
        itemBuilder: (BuildContext context, int index) {
          return editValueItem(index);
        },
        itemCount: editTitleList.length,
      ),
    );
  }
}

/*
*
* 编辑资料---输入昵称
*
* */
class InputUnickName extends StatefulWidget {
  const InputUnickName({Key key}) : super(key: key);

  @override
  _InputUnickNameState createState() => _InputUnickNameState();
}

class _InputUnickNameState extends State<InputUnickName> {
  //获取输入框输入的数据
  String _textValue;

  ///APPBar头部状态栏
  AppBar appBarWiget() {
    return AppBar(
      ///去掉APPBar的箭头
      automaticallyImplyLeading: false,
      backgroundColor: Colors.white,
      elevation: 0,
      leading: GestureDetector(
        onTap: () {
          Navigator.pop(context);
        },
        child: Container(
          child: Icon(
            Icons.arrow_back_ios,
            color: Colors.black,
            size: 24,
          ),
        ),
      ),
      centerTitle: true,
      title: Text(
        "编辑昵称",
        style: TextStyle(fontSize: 18, color: Color.fromRGBO(17, 17, 17, 1)),
      ),
      actions: [
        Container(
          width: MediaQuery.of(context).size.width - 299,
          padding: EdgeInsets.only(top: 8.5, bottom: 7.5),
          child: GestureDetector(
            onTap: () {
              print("-------------------确定--------------------");
                Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return EditDataPage(nickName: _textValue,);
                }));
            },
            child: Container(
              margin: EdgeInsets.only(right: 16),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Color.fromRGBO(255, 64, 89, 1),
                borderRadius: BorderRadius.circular(14),
              ),
              child: Text(
                "确定",
                style: TextStyle(fontSize: 14, color: Colors.white),
              ),
            ),
          ),
        ),
      ],
    );
  }

  ///昵称输入框
  Widget nickInput() {
    return Container(
      margin: EdgeInsets.only(top: 17),
      child: TextField(
        autofocus: false,
        cursorColor: Color.fromRGBO(153, 153, 153, 1),
        maxLines: 1,
        maxLength: 15,
        decoration: InputDecoration(
          counterText: '',
          hintText: '戳这里编辑昵称',
          hintStyle: TextStyle(color: Colors.grey),
          hintMaxLines: 1,
          border: InputBorder.none, //去掉下滑线
        ),
        onChanged: (val) {
          setState(() {
            _textValue = val;
          });
        },
      ),
    );
  }

  ///提示信息
  Widget promptInformation() {
    return Container(
      margin: EdgeInsets.only(top: 12),
      child: Row(
        children: <Widget>[
          Container(
            child: Text(
              "0-15个字符，起个好听的名字吧~",
              style: TextStyle(
                  fontSize: 14, color: Color.fromRGBO(153, 153, 153, 1)),
            ),
          ),
          Spacer(),
          Container(
            child: Text(
              "${15 - (_textValue != null ? _textValue.length : 0)}",
              style: TextStyle(
                  fontSize: 14, color: Color.fromRGBO(153, 153, 153, 1)),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBarWiget(),
      body: Container(
        width: MediaQuery.of(context).size.width - 32,
        margin: EdgeInsets.only(left: 16),
        child: Column(
          children: <Widget>[
            //昵称输入框
            nickInput(),
            //提示信息
            promptInformation(),
          ],
        ),
      ),
    );
  }
}
